package balmysundaycandy.more.low.level.logger;

import com.google.appengine.repackaged.com.google.io.protocol.ProtocolMessage;

/**
 * log contents for apiproxy.makesynccall or makeasynccall.
 * 
 * @author marblejenka
 * 
 */
public class ProtocolBufferLogContents {
	private static final String separator = System.getProperty("line.separator");

	/**
	 * key for generate protocol buffer class
	 */
	private ProtocolBufferKey key;

	/**
	 * request as byte
	 */
	private byte[] request;

	/**
	 * response as byte
	 */
	private byte[] response;

	public ProtocolBufferLogContents key(ProtocolBufferKey key) {
		this.key = key;
		return this;
	}

	public ProtocolBufferLogContents request(byte[] request) {
		this.request = request;
		return this;
	}

	public ProtocolBufferLogContents response(byte[] response) {
		this.response = response;
		return this;
	}

	@Override
	public String toString() {
		// build request message protocol buffer
		ProtocolMessage<?> requestmessage = ProtocolBufferObjectHolder.protocolmessagerequests.get(key).clone();
		requestmessage.mergeFrom(request);

		// build response message protocol buffer
		ProtocolMessage<?> responsemessage = ProtocolBufferObjectHolder.protocolmessageresponses.get(key).clone();
		responsemessage.mergeFrom(response);

		// convert to string
		return new StringBuilder().append("************************************").append(separator).append("== servicename ==").append(separator).append(key.getServicename())
				.append(separator).append(separator).append("== methodname ==").append(separator).append(key.getMethodname()).append(separator).append(separator).append(
						"== request ==").append(separator).append(requestmessage.toString()).append(separator).append("== response ==").append(separator).append(
						responsemessage.toString()).append(separator).toString();
	}
}
